package com.alameda.service.impl.history;

import com.alameda.common.annotation.DataScope;
import com.alameda.common.base.enums.DeleteFlagEnum;
import com.alameda.common.utils.CollectionUtils;
import com.alameda.common.utils.DateUtils;
import com.alameda.common.utils.ObjectUtils;
import com.alameda.common.utils.PubUtils;
import com.alameda.facade.history.ApVideoHistoryFacade;
import com.alameda.facade.video.ApVideoFacade;
import com.alameda.history.dto.ApVideoHistoryDTO;
import com.alameda.history.vo.ApVideoHistoryVO;
import com.alameda.mapper.history.ApVideoHistoryMapper;
import com.alameda.pojo.history.ApVideoHistoryDO;
import com.alameda.service.history.ApVideoHistoryService;
import com.alameda.statement.enums.AdSystemRecordEnum;
import com.alameda.video.dto.ApVideoDTO;
import com.alameda.video.vo.ApVideoVO;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 视频播放历史记录 业务逻辑层 具体实现
 *
 * @author Lv
 * @date 2024/10/09
 */
@Slf4j
@Service
public class ApVideoHistoryServiceImpl extends ServiceImpl<ApVideoHistoryMapper, ApVideoHistoryDO> implements ApVideoHistoryService {

    @Autowired
    private ApVideoHistoryMapper apVideoHistoryMapper;

    @Autowired
    private ApVideoHistoryFacade apVideoHistoryFacade;

    @Autowired
    private ApVideoFacade apVideoFacade;

    /**
     * 加载视频观看记录
     *
     * @param apVideoHistoryDTO 视频播放历史记录信息
     * @return
     */
    @Override
    public List<ApVideoHistoryVO> loadHistory(ApVideoHistoryDTO apVideoHistoryDTO) {
        List<ApVideoHistoryDO> apVideoHistoryDOList = apVideoHistoryMapper.findByVideoNameLike(apVideoHistoryDTO.getVideoName(), apVideoHistoryDTO.getLoadCount());
        apVideoHistoryDOList = Optional.ofNullable(apVideoHistoryDOList).orElse(CollectionUtils.newArrayList());

        return apVideoHistoryDOList.stream()
                .map(item -> ObjectUtils.convert(item, ApVideoHistoryVO.class))
                .peek(item -> {
                    // 处理视频文件名称
                    String videoName = apVideoFacade.dealVideoName(item.getVideoName());
                    // 格式化播放时刻
                    String formatTime = DateUtils.formatLocalDateTimeToYmdHms(item.getUpdateTime());
                    item.setVideoName(videoName);
                    item.setTime(formatTime);
                })
                .collect(Collectors.toList());
    }

    /**
     * 保存视频播放记录
     *
     * @param apVideoHistoryDTO 视频播放历史记录信息
     * @return
     */
    @DataScope
    @Override
    public Boolean saveVideoHistory(ApVideoHistoryDTO apVideoHistoryDTO) {
        ApVideoHistoryDO apVideoHistoryDO = this.lambdaQuery().eq(ApVideoHistoryDO::getVideoId, apVideoHistoryDTO.getVideoId()).one();

        apVideoHistoryDO = Optional.ofNullable(apVideoHistoryDO).orElse(initApVideoHistory(apVideoHistoryDTO.getVideoId()));
        apVideoHistoryDO.setPlayMoment(apVideoHistoryDTO.getPlayMoment());
        apVideoHistoryDO.setUpdateTime(LocalDateTime.now());

        log.info("视频播放记录信息:{}", JSONObject.toJSONString(apVideoHistoryDO));
        apVideoFacade.saveRecord(AdSystemRecordEnum.BUSINESS_COUNT);
        return this.saveOrUpdate(apVideoHistoryDO);
    }

    /**
     * 删除播放记录
     *
     * @param idList 媒体文件id集合
     * @return
     */
    @Override
    public Boolean removeByMediaIdList(List<Long> idList) {
        log.info("删除播放记录:{}", JSONObject.toJSONString(idList));

        return PubUtils.isEmpty(idList)
                ? Boolean.TRUE
                : this.lambdaUpdate()
                .in(ApVideoHistoryDO::getVideoId, idList)
                .set(ApVideoHistoryDO::getDeleteFlag, DeleteFlagEnum.IS_DELETE.getFlag())
                .update();
    }

    /**
     * 修改播放记录信息
     *
     * @param apVideoHistoryDTO 视频播放历史记录信息
     * @return
     */
    @Override
    public Boolean updateMediaHistory(ApVideoHistoryDTO apVideoHistoryDTO) {
        log.info("修改播放参数信息:{}", JSONObject.toJSONString(apVideoHistoryDTO));
        return this.lambdaUpdate()
                .set(PubUtils.isNotEmpty(apVideoHistoryDTO.getVideoName()), ApVideoHistoryDO::getVideoName, apVideoHistoryDTO.getVideoName())
                .set(PubUtils.isNotEmpty(apVideoHistoryDTO.getVideoCover()), ApVideoHistoryDO::getVideoCover, apVideoHistoryDTO.getVideoCover())
                .eq(ApVideoHistoryDO::getVideoId, apVideoHistoryDTO.getVideoId())
                .update();
    }

    /**
     * 读取播放历史记录信息
     *
     * @param apVideoHistoryDTO 视频播放历史记录信息
     * @return
     */
    @Override
    public List<ApVideoHistoryVO> loadApVideoHistory(ApVideoHistoryDTO apVideoHistoryDTO) {
        ApVideoHistoryDO apVideoHistoryDO = ObjectUtils.convert(apVideoHistoryDTO, ApVideoHistoryDO.class);
        List<ApVideoHistoryDO> apVideoHistoryDOList = this.list(apVideoHistoryDO);
        return ObjectUtils.convertList(apVideoHistoryDOList, ApVideoHistoryVO.class);
    }

    /**
     * 批量修改播放历史记录信息
     *
     * @param apVideoHistoryDTOList 视频播放历史记录信息
     * @return
     */
    @Override
    public Boolean updateMediaHistoryBatch(List<ApVideoHistoryDTO> apVideoHistoryDTOList) {
        List<ApVideoHistoryDO> apVideoHistoryDOList = ObjectUtils.convertList(apVideoHistoryDTOList, ApVideoHistoryDO.class);
        return this.updateBatchById(apVideoHistoryDOList);
    }

    /**
     * 初始化历史记录
     *
     * @param videoId 视频文件id
     * @return
     */
    private ApVideoHistoryDO initApVideoHistory(Long videoId) {
        ApVideoVO apVideoVO = apVideoHistoryFacade.findByVideoId(ApVideoDTO.builder().id(videoId).build());

        return ApVideoHistoryDO.builder()
                .videoId(apVideoVO.getId())
                .videoName(apVideoVO.getVideoName())
                .videoCover(apVideoVO.getVideoCover())
                .build();
    }
}
